package com.shanghaionstar.smart.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.sqlite.entities.LandingsPageEntity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.iview.ISmartCaptureView;
import com.shanghaionstar.smart.service.SmartHomeService;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.concurrent.TimeUnit;

import rx.functions.Action1;

/**
 * Created by 7828wc on 2016/6/22.
 */
public class SmartCapturePresenter extends BasePresenter<LandingsPageEntity> {
    private BaseActivity mContext;
    private ISmartCaptureView mSmartCaptureView;
    private SmartHomeService service;

    public SmartCapturePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<LandingsPageEntity> view) {
        super.setView(view);
        mSmartCaptureView = (ISmartCaptureView) view;
        mContext = (BaseActivity) mSmartCaptureView.onActivity();
        service = new SmartHomeService(mContext);
    }

//    /**
//     * 根据是否存在设备列表显示相关fragment
//     */
//    public void authQRcode(String qrCode) {
//        mSmartCaptureView.setLoading(true);
//        subscription = service.authQRCode(NetConnectUtil.getAuthRequestEntity(mContext),qrCode)
//                .subscribeOn(mvpScheduler.backgroundThread())
//                .observeOn(mvpScheduler.mainThread())
//                .subscribe(new Action1<BaseResponse>() {
//                    @Override
//                    public void call(BaseResponse mQRCodeResponse) {
//                        mSmartCaptureView.setLoading(false);
//                        mSmartCaptureView.onReponseSuccess(mQRCodeResponse);
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        mSmartCaptureView.setLoading(false);
//                        if (throwable != null)
//                            mSmartCaptureView.onResponseError((OnStarException) throwable);
//                    }
//                });
//    }



    /**
     * 根据是否存在设备列表显示相关fragment
     */
    public void authQRcode(String qrCode) {
        mSmartCaptureView.setLoading(true);
        subscription = service.authQRCode(NetConnectUtil.getAuthRequestEntity(mContext),qrCode)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
//                .delay(5, TimeUnit.SECONDS)
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse mQRCodeResponse) {
                        mSmartCaptureView.setLoading(false);
                        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Mobile_ScanBarCode_Success.getStringValue());
                        mSmartCaptureView.onReponseSuccess(mQRCodeResponse);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mSmartCaptureView.setLoading(false);
                        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Mobile_ScanBarCode_Fail.getStringValue());
                        if (throwable != null) {
                            mSmartCaptureView.onResponseError((OnStarException) throwable);
                        } else {
                            mSmartCaptureView.onResponseError(null);
                        }
                    }
                });
    }



}



